<template>
  <div class>
    <div class="card tr mb2">
      <el-tooltip content="显示字段筛选" placement="bottom" effect="light">
        <el-popover placement="bottom" width="200" trigger="click">
          <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
          <el-checkbox-group v-model="showItemList" @change="handleCheckedChange">
            <el-checkbox v-for="(item,key) in allItemList" :key="key" :label="item">{{item.name}}</el-checkbox>
          </el-checkbox-group>
          <el-button slot="reference" icon="el-icon-menu" size="mini" type="primary" circle></el-button>
        </el-popover>
      </el-tooltip>
      <el-tooltip content="展开/收起表单" placement="bottom" effect="light">
        <el-button icon="el-icon-search" @click="formShow= !formShow" size="mini" type="primary" circle></el-button>
      </el-tooltip>
      <el-tooltip content="下载当前表格数据" placement="bottom" effect="light">
        <el-button icon="el-icon-download" @click="handleDownload" size="mini" type="primary" circle></el-button>
      </el-tooltip>
      <el-button size="mini" type="primary" icon="el-icon-plus" @click="showDialog">添加</el-button>
    </div>
    <div class="card" v-show="formShow">
      <el-form size="mini" :style="style" :model="ruleForm" ref="ruleForm" inline label-width="100px" class="demo-ruleForm">
        <my-form-item label="指标类型编号" prop="setCode">
          <el-input v-model="ruleForm.setCode" size="mini"></el-input>
        </my-form-item>
        <my-form-item label="指标类型名称" prop="setName">
          <el-input v-model="ruleForm.setName" size="mini"></el-input>
        </my-form-item>
        <my-form-item>
          <el-button size="mini" type="primary" @click="submitForm('ruleForm')">查询</el-button>
          <el-button size="mini" @click="resetForm('ruleForm')">重置</el-button>
        </my-form-item>
      </el-form>
    </div>
    <el-table stripe :data="tableData" border style="width: 100%">
      <el-table-column type="index" width="60px" align="center" fixed="left" label="序号"></el-table-column>
      </el-table-column>
      <el-table-column v-for="(item,index) in showItemList" :width="item.width || '' " :key="index" :label="item.name">
        <template slot-scope="scope">
          <template v-if="!item.formatter">{{ scope.row[item.key] }}</template>
          <template v-else>
            <span v-text="item.formatter(scope.row, item)"></span>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="250px" fixed="right" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="addZbmx(scope.row)">指标明细</el-button>
          <el-button size="mini" type="primary" @click="showEditDialog(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-card style="text-align:right;">
      <el-pagination @size-change="sizeChange" @current-change="currentChange" :current-page="currentPage" :page-sizes="[ 10, 20,30, 40]" :page-size="pageSize" background layout="total, sizes, prev, pager, next" :total="total"></el-pagination>
    </el-card>
    <!-- --------------------------------------------------------添加弹出框 -->
    <el-dialog title="新增管理" width="300px" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
      <el-form size="mini" :style="style" :model="addForm" ref="addForm" :rules="rules" class="demo-ruleForm">

        <!--基础表新增-->
        <my-form-item label="指标类型编号" :label-width="formLabelWidth" prop="setCode">
          <el-input v-model="addForm.setCode" size="mini" autocomplete="off"></el-input>
        </my-form-item>
        <my-form-item label="指标类型名称" :label-width="formLabelWidth" prop="setName">
          <el-input v-model="addForm.setName" placeholder="必填项" size="mini" autocomplete="off"></el-input>
        </my-form-item>
        <my-form-item label="启用状态" :label-width="formLabelWidth" prop="isUse">
          <el-radio-group v-model="addForm.isUse" size="mini">
            <el-radio label="1">启用</el-radio>
            <el-radio label="0">不启用</el-radio>
          </el-radio-group>
        </my-form-item>
        <!-- 文件上传 -->
        <my-form-item :label-width="formLabelWidth" label="备注" prop="remarks">
          <el-input type="textarea" v-model="addForm.remarks"></el-input>
        </my-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" size="mini">取 消</el-button>
        <el-button type="primary" @click="handleAdd()" size="mini">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
const defaultFormThead = [
  { key: "setCode", name: "指标类型编号" },
  { key: "setName", name: "指标类型名称" },
  { key: "remarks", name: "备注" },
  {
    key: "isUse",
    name: "启用状态",
    formatter: row => {
      if (row.tmpType == "1") {
        return "是";
      } else {
        return "否";
      }
    }
  }
];
import EnumSelector from "@/pages/comm/enumSelector";
export default {
  components: { EnumSelector },
  data() {
    return {
      checkAll: true,
      isIndeterminate: false,
      allItemList: defaultFormThead,
      showItemList: defaultFormThead,
      formShow: false,
      dialogFormVisible: false,
      dialogEditVisible: false,
      offForm: true, //是否折叠
      folding: false, //表单是否超过一行
      formLabelWidth: "110px",
      tableData: [],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      ruleForm: {
        setCode: "",
        setName: ""
      },
      style: {},
      rules: {
        setCode: [
          {
            required: true,
            message: "请输入指标类型编号",
            trigger: "blur"
          }
        ],
        setName: [
          {
            required: true,
            message: "请输入指标类型名称",
            trigger: "blur"
          }
        ],
        isUse: [
          {
            required: true,
            message: "请选择是否启用",
            trigger: "change"
          }
        ]
      },
      addForm: {
        id: "",
        setCode: "",
        setName: "",
        isUse: "",
        remarks: ""
      }
    };
  },

  mounted() {
    this.init();
    let formHeight = this.$refs.ruleForm.$el.clientHeight;
    if (formHeight > 62) {
      this.folding = true;
      this.style = {
        height: "62px"
      };
    } else {
      this.folding = false;
      this.style = {
        height: "auto"
      };
    }
  },
  computed: {},
  watch: {
    offForm: function(v) {
      if (v) {
        this.style = {
          height: "62px"
        };
      } else {
        this.style = {
          height: "auto"
        };
      }
    }
  },
  methods: {
    handleDownload() {
      this.downloadLoading = true;
      import("@/vendor/Export2Excel").then(excel => {
        const tHeader = ["指标类型编号", "指标类型名称", "启用状态"];
        const filterVal = ["setCode", "setName", "isUse"];
        const data = this.formatJson(filterVal, this.tableData);
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: "table-list"
        });
        this.downloadLoading = false;
      });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v =>
        filterVal.map(j => {
          if (j === "timestamp") {
            return parseTime(v[j]);
          } else {
            return v[j];
          }
        })
      );
    },
    handleCheckedChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === defaultFormThead.length;
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < defaultFormThead.length;
    },
    handleCheckAllChange(val) {
      this.showItemList = val ? defaultFormThead : [];
      this.isIndeterminate = false;
    },
    sizeChange(n) {
      this.pageSize = n;
      this.init();
    },
    currentChange(n) {
      this.currentPage = n;
      this.init();
    },
    init() {
      let searchForm = {};
      searchForm.setCode = this.ruleForm.setCode;
      searchForm.setName = this.ruleForm.setName;
      let params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        ...searchForm
      };
      this.$api
        .formPost("/api/biz/finIndicator/doGetList", params)
        .then(res => {
          if (res.retHead.code == "0000000") {
            this.tableData = res.retBody.list;
            this.total = res.retBody.total;
            this.currentPage = res.retBody.pageNum;
          }
        });
    },
    showDialog() {
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.resetForm("addForm");
      });
      this.form = {};
    },
    showEditDialog(index, row) {
      this.addForm.id = row.id;
      this.addForm.setCode = row.setCode;
      this.addForm.setName = row.setName;
      this.addForm.isUse = row.isUse;
      this.addForm.remarks = row.remarks;
      this.dialogFormVisible = true;
    },
    handleAdd() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          let addFormParams = {};
          addFormParams.setCode = this.addForm.setCode;
          addFormParams.setName = this.addForm.setName;
          addFormParams.isUse = this.addForm.isUse;
          addFormParams.remarks = this.addForm.remarks;
          addFormParams.id = this.addForm.id;
          this.$api
            .formPost("/api/biz/finIndicator/doSaveFinSet", addFormParams)
            .then(res => {
              if (res.retHead.code == "0000000") {
                this.$message({
                  message: "添加成功！",
                  type: "success"
                });
                this.dialogFormVisible = false;
                this.init();
              } else {
                this.$message({
                  message: "添加失败！",
                  type: "error"
                });
              }
            });
        } else {
          this.$message.error("请完善表单");
          return false;
        }
      });
    },

    //新增指标明细
    addZbmx(row) {
      console.log(row);
      this.$router.push({
        path: "/insert/set",
        query: {
          setId: row.id,
          setCode: row.setCode,
          setName: row.setName,
          isUse: row.isUse
        }
      });
    },
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$api
            .formPost("/api/biz/finIndicator/doRmByFinSet", {
              id: row.id
            })
            .then(res => {
              if (res.retHead.code == "0000000") {
                this.$message({
                  message: "操作成功！",
                  type: "success"
                });
                this.init();
              } else {
                this.$message({
                  message: "操作失败！",
                  type: "error"
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    submitForm(formName) {
      this.init();
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  created() {}
};
</script>
<style lang='scss' scoped>
.btns {
  text-align: right;
}
.icon-arrow {
  position: absolute;
  bottom: 5px;
  left: 0;
  width: 100%;
  height: 1px;
  background-color: #f1f1f1;
  i {
    background-color: #fff;
    position: absolute;
    color: #999;
    bottom: -7px;
    margin-left: 50%;
    transform: translateX(-50%);
    cursor: pointer;
  }
}
</style>